﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Opennav.MapGenerator.Map.Labels
{
    public class Label : IComparable
    {
        private int _offset = -1;

        public Label(byte[] value)
        {
            this.Value = value;
        }

        public byte[] Value { get; private set; }

        public string DebugValue
        {
            get
            {
                UTF8Encoding encoding = new UTF8Encoding();
                return encoding.GetString(Value);
            }
        }

        public int CompareTo(object obj)
        {
            Label other = obj as Label;
            if (this == other) return 0;
            UTF8Encoding encoding = new UTF8Encoding();
            string v1 = encoding.GetString(Value).ToUpper();
            string v2 = encoding.GetString(other.Value).ToUpper();
            return v1.CompareTo(v2);
        }

        public int Offset
        {
            get
            {
                Debug.Assert(_offset >= 0);
                return _offset;
            }
            set
            {
                this._offset = value;
            }
        }

        public int SavedLength
        {
            get
            {
                return Value.Length + sizeof(ushort);
            }
        }

        public void Write(Stream stream)
        {
            BinaryWriter writer = new BinaryWriter(stream);
            Debug.Assert(Value.Length <= ushort.MaxValue);
            ushort length = (ushort)Value.Length;
            writer.Write(length);
            writer.Write(Value);
        }
    }
}
